var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    offsetRecord: {
      'index': -1,
      'startX': 0,
      'offset': 0
    }, // 偏移记录
    deleteButtonWidth: 200, // 删除按钮的宽度(rpx)
    pixelScale: 1,
    page: 0,
    type: 1,
    privateInfo: null,
    num: '',
    message: '',
    new_list:'',
    new_private:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    wx.request({
      // url: app.url + '/index.php/mobile/Activity_51/get_msg',
      url: app.urlData.getURL + '/index.php/api/SApi/index/key/' + app.urlData.key + '/m/get_msg/wid/' + app.urlData.wid + '/sign/' + wx.getStorageSync('sign'),
      method: 'GET',
      // header: {
      //   "Content-Type": "application/x-www-form-urlencoded"
      // },
      data: {
        // user_id: wx.getStorageSync('user_id'),
        page: that.data.page
      },
      success: (res) => {
        res.data.data.map(item => {
          item['nums'] = Math.floor(Math.random() * 5)
        })
        that.setData({
          items: res.data.data,
          privateInfo: res.data.private,
          message: res.data.data,
          new_list: res.data.new_list,
          new_private: res.data.new_private
        })
      }
    })
    // wx.request({
    //   // url: app.url + '/index.php/mobile/Activity_51/delete_msg',
    //   url: app.url2 + '/index.php/api/SApi/index/key/' + app.urlData.key + '/m/delete_msg/wid/' + app.urlData.wid + '/sign/' + wx.getStorageSync('sign'),
    //   method: 'GET',
    //   // header: {
    //   //   "Content-Type": "application/x-www-form-urlencoded"
    //   // },
    //   data: {
    //     message_id: 'all',
    //     type: '1'
    //   },

    // })


    wx.hideTabBarRedDot({
      index: 1
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log(this.data.items)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 表格cell触摸开始事件
   */
  onTableCellTouchStart: function(event) {
    if (event.changedTouches.length != 1) return;
    let index = event.currentTarget.dataset.index;
    let x = event.changedTouches[0].clientX;
    this.setData({
      offsetRecord: {
        'index': index,
        'startX': x,
        'offset': 0
      }
    });
  },

  /**
   * 表格cell触摸移动事件
   */
  onTableCellTouchMove: function(event) {
    if (event.changedTouches.length != 1) return;
    let index = event.currentTarget.dataset.index;
    let record = this.data.offsetRecord;
    if (index != Reflect.get(record, 'index')) {
      return;
    }
    let clientX = event.changedTouches[0].clientX;
    let startX = Reflect.get(record, 'startX');
    let distance = Math.max(Math.min((startX - clientX) * this.data.pixelScale, this.data.deleteButtonWidth), 0);
    Reflect.set(record, 'offset', distance);
    this.setData({
      offsetRecord: record
    });
  },

  /**
   * 表格cell触摸结束事件
   */
  onTableCellTouchEnd: function(event) {
    if (event.changedTouches.length != 1) return;
    let index = event.currentTarget.dataset.index;
    let record = this.data.offsetRecord;

    if (index == Reflect.get(record, 'index')) {
      let offset = Reflect.get(record, 'offset');
      if (offset >= this.data.deleteButtonWidth / 2) {
        Reflect.set(record, 'offset', this.data.deleteButtonWidth);
      } else {
        record = null;
      }
      this.setData({
        offsetRecord: record
      });
    }
  },

  /**
   * 表格cell删除按钮点击事件
   */
  onDeleteButtonTapped: function(e) {
    var that = this
    console.log(e.currentTarget.dataset.id)
    var a = {
      index: -1,
      startX: 0,
      sffset: 0
    }
    wx.request({
      // url: app.url + '/index.php/mobile/Activity_51/delete_msg',
      url: app.urlData.getURL + '/index.php/api/SApi/index/key/' + app.urlData.key + '/m/delete_msg/wid/' + app.urlData.wid + '/sign/' + wx.getStorageSync('sign'),
      method: 'get',
      // header: {
      //   "Content-Type": "application/x-www-form-urlencoded"
      // },
      data: {
        message_id: e.currentTarget.dataset.id,
        // user_id: wx.getStorageSync('user_id'),
        type: 1
      },
      success: (res) => {
        console.log(res.data.status)
        that.setData({
          offsetRecord: a
        })
        wx.request({
          url: app.urlData.getURL + '/index.php/api/SApi/index/key/' + app.urlData.key + '/m/get_msg/wid/' + app.urlData.wid + '/sign/' + wx.getStorageSync('sign'),
          method: 'GET',
          // header: {
          //   "Content-Type": "application/x-www-form-urlencoded"
          // },
          data: {
            // user_id: wx.getStorageSync('user_id')
          },
          success: (res) => {
            wx.showToast({
              title: '删除成功',
              icon: 'none',
              duration: 1000
            })
            console.log(res.data.data)
            res.data.data.map(item => {
              item['nums'] = Math.floor(Math.random() * 5)
            })
            that.setData({
              items: res.data.data
            })
          }
        })


      }
    })
  },
  toMyPrize(e) {

    wx.navigateTo({
      url: '../productDetail/productDetail?id=' + e.currentTarget.dataset.id
    })
  },
  //下拉加载更多
  // onReachBottom: function() {
  //   var that = this
  //   wx.request({
  //     url: app.url2 + '/index.php/api/SApi/index/key/' + app.urlData.key + '/m/get_msg/wid/' + app.urlData.wid + '/sign/' + wx.getStorageSync('sign'),
  //     method: 'GET',
  //     // header: {
  //     //   "Content-Type": "application/x-www-form-urlencoded"
  //     // },
  //     data: {
  //       // user_id: wx.getStorageSync('user_id'),
  //       page: that.data.page + 1
  //     },
  //     success: (res) => {
  //       // if (res.data.data.length > 0) {
  //       that.setData({
  //         items: that.data.items.concat(res.data.data),
  //         page: that.data.page + 1
  //       })
  //       // } else {
  //       // }


  //     }
  //   })
  // },
  systomInfo() {
    var that = this
    if (that.data.type == 2) {
      that.setData({
        type: 1,
        new_private:0
      })
    } else {

    }

  },
  privateInfo() {
    var that = this
    if (that.data.type == 1) {
      that.setData({
        type: 2,
        new_list:0

      })
    } else {

    }
  },
  deleteAll() {
    var that = this
    wx.showModal({
      title: '清空消息',
      content: '是否确认清空系统消息？',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            // url: app.url + '/index.php/mobile/Activity_51/delete_msg',
            url: app.urlData.getURL + '/index.php/api/SApi/index/key/' + app.urlData.key + '/m/delete_msg/wid/' + app.urlData.wid + '/sign/' + wx.getStorageSync('sign'),
            method: 'get',
            // header: {
            //   "Content-Type": "application/x-www-form-urlencoded"
            // },
            data: {
              message_id: 'all',
              user_id: wx.getStorageSync('user_id'),
              type: 0
            },
            success: (res) => {
              wx.request({
                // url: app.url + '/index.php/mobile/Activity_51/get_msg',
                url: app.urlData.getURL + '/index.php/api/SApi/index/key/' + app.urlData.key + '/m/get_msg/wid/' + app.urlData.wid + '/sign/' + wx.getStorageSync('sign'),
                method: 'get',
                // header: {
                //   "Content-Type": "application/x-www-form-urlencoded"
                // },
                data: {
                  // user_id: wx.getStorageSync('user_id')
                },
                success: (res) => {
                  wx.showToast({
                    title: '消息清空完毕',
                    icon: 'none',
                    duration: 1000
                  })
                  console.log(res.data.data)
                  that.setData({
                    items: res.data.data,
                    privateInfo: res.data.private
                  })
                }
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  copeyWechat: function(e) {
    let wechat = e.currentTarget.dataset.id
    //设置剪切内容
    wx.setClipboardData({
      data: wechat,
      success: function(res) {
        wx.getClipboardData({
          success: function(res) {
            console.log(res.data)
          }
        })
      }
    })
  }
})
